﻿@using OA.Models
@{
    var ControllerName = "Department";
}

@(
    Html.Kendo().Grid<Department>()
    .Name("grid")
    .HtmlAttributes(new { style = "height:99%" })
    .Filterable()
    .Pageable()
    .Selectable()
    .Scrollable()
    .ToolBar(
        toolbar => {
            toolbar.Template(
                @<button class='k-button k-button-icontext k-grid-add' onclick="openInsertWindow()">
                    <span class="k-icon k-i-add"></span>新增
                </button>
            );
        }
    )
    .Columns(
        columns =>
        {
            columns.Bound(c => c.Name);
            columns.Bound(c => c.Parent).ClientTemplate("#= Parent != null ? Parent.Name : '' #");
            columns.Bound(c => c.Remarks);
            columns.Bound(c => c.Sort);
            columns.Command(c =>
            {
                c.Custom("edit").Text("编辑").Click("openEditWindow");
                c.Custom("delete").Text("删除").Click("deleteEntity");
            }).Width(190);
        }
    )
    .DataSource(
        dataSource => dataSource
            .Ajax()
            .ServerOperation(true)
            .PageSize(18)
            .Model(
                model =>
                {
                    model.Id(p => p.Id);
                }
            )
            .Read(read => read.Action("Read", ControllerName))
            .Events(events => events.Error("dataSourceError"))
    )
)

<script>
    function openInsertWindow() {
        openWindow('新建部门', '/@ControllerName/Update', { Id: 0 });
    }

    function openEditWindow(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        var data = { Id: dataItem.Id };
        openWindow('编辑部门', '/@ControllerName/Update', data)
    }

    function deleteEntity(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        kendo.confirm("你确定要删除该部门吗?", "删除部门").then(function () {
            $.post({
                url: "/@ControllerName/Delete",
                dataType: 'json',
                data: { Id: dataItem.Id },
                success: function (response) {
                    $("#grid").data("kendoGrid").dataSource.read();
                    openNotification(response.Message, "success");
                },
                error: ajaxError
            })
        })
    }
</script>